package com.blog.myUtils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class UploadHelper {

    public static String uploadImage(String pathRoot, MultipartFile file) {
        //生成uuid作为文件名称
        String path = "";
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String contentType = file.getContentType();
        //获得文件后缀名称
        String tail = contentType.substring(contentType.indexOf("/") + 1);
        path = "/static/images/" + uuid + "." + tail;
        try {
            file.transferTo(new File(pathRoot + path));
            System.out.println(pathRoot + path);
            return path;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
